Skip to content

Commit 6113458

Browse files
committed
Morphed ForwardingEquality into LazyForwardingEquality.
1 parent 7b6aea7 commit 6113458

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

src/main/java/com/diffplug/gradle/spotless/ForwardingEquality.java renamed to src/main/java/com/diffplug/gradle/spotless/LazyForwardingEquality.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,42 @@
2222
import java.io.Serializable;
2323
import java.util.Objects;
2424

25+
import javax.annotation.Nonnull;
26+
import javax.annotation.Nullable;
27+
2528
/**
2629
* Implements equality, hashcode, and serialization entirely in terms
27-
* of the given key. It is appropriate for subclasses of this class
28-
* to use `@SuppressWarnings("serial")`.
30+
* of a lazily-computed key.
2931
*/
3032
@SuppressWarnings("serial")
31-
public abstract class ForwardingEquality<T extends Serializable> implements Serializable {
32-
private T key;
33+
public abstract class LazyForwardingEquality<T extends Serializable> implements Serializable {
34+
/** Null indicates that the key has not yet been set. */
35+
@Nullable
36+
private transient volatile T key;
3337

34-
ForwardingEquality(T key) {
35-
this.key = Objects.requireNonNull(key);
36-
}
38+
/**
39+
* This function is guaranteed to be called at most once.
40+
* If the key is never required, then it will never be called at all.
41+
*/
42+
@Nonnull
43+
protected abstract T calculateKey();
3744

38-
protected T key() {
45+
/** Returns the underlying key, possibly triggering a call to {{@link #calculateKey()}. */
46+
@Nonnull
47+
protected final T key() {
48+
// double-checked locking for lazy evaluation of calculateKey
49+
if (key == null) {
50+
synchronized (this) {
51+
if (key == null) {
52+
key = calculateKey();
53+
}
54+
}
55+
}
3956
return key;
4057
}
4158

4259
private void writeObject(ObjectOutputStream out) throws IOException {
43-
out.writeObject(key);
60+
out.writeObject(key());
4461
}
4562

4663
@SuppressWarnings("unchecked")
@@ -54,18 +71,18 @@ private void readObjectNoData() throws ObjectStreamException {
5471
}
5572

5673
@Override
57-
public boolean equals(Object other) {
74+
public final boolean equals(Object other) {
5875
if (other == null) {
5976
return false;
6077
} else if (getClass().equals(other.getClass())) {
61-
return key.equals(((ForwardingEquality<?>) other).key);
78+
return key().equals(((LazyForwardingEquality<?>) other).key());
6279
} else {
6380
return false;
6481
}
6582
}
6683

6784
@Override
68-
public int hashCode() {
69-
return key.hashCode();
85+
public final int hashCode() {
86+
return key().hashCode();
7087
}
7188
}

src/test/java/com/diffplug/gradle/spotless/ForwardingEqualityTest.java renamed to src/test/java/com/diffplug/gradle/spotless/LazyForwardingEqualityTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,48 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static com.diffplug.common.testing.SerializableTester.*;
19+
1820
import org.junit.Test;
1921

2022
import com.diffplug.common.testing.EqualsTester;
21-
import com.diffplug.common.testing.SerializableTester;
2223

23-
public class ForwardingEqualityTest {
24+
@SuppressWarnings("serial")
25+
public class LazyForwardingEqualityTest {
2426
static Str s(String key) {
2527
return new Str(key);
2628
}
2729

28-
@SuppressWarnings("serial")
29-
static class Str extends ForwardingEquality<String> {
30+
static Other o(String key) {
31+
return new Other(key);
32+
}
33+
34+
static class Str extends LazyForwardingEquality<String> {
35+
private String key;
36+
3037
Str(String key) {
31-
super(key);
38+
this.key = key;
3239
}
33-
}
3440

35-
static Int i(int key) {
36-
return new Int(key);
41+
@Override
42+
protected String calculateKey() {
43+
return key;
44+
}
3745
}
3846

39-
@SuppressWarnings("serial")
40-
static class Int extends ForwardingEquality<Integer> {
41-
Int(int key) {
47+
static class Other extends Str {
48+
Other(String key) {
4249
super(key);
4350
}
4451
}
4552

4653
@Test
4754
public void testEquality() {
4855
new EqualsTester()
49-
.addEqualityGroup(s("hello"), s("hello"), s("h" + "ello"))
50-
.addEqualityGroup(s("world"), s("world"), s("wor" + "ld"))
51-
.addEqualityGroup(i(3), i(3), i(1 + 2))
52-
.addEqualityGroup(i(-6), i(-6), i(1 - 7))
56+
.addEqualityGroup(s("hello"), reserializeAndAssert(s("hello")))
57+
.addEqualityGroup(s("world"), reserializeAndAssert(s("world")))
58+
.addEqualityGroup(o("hello"), reserializeAndAssert(o("hello")))
59+
.addEqualityGroup(o("world"), reserializeAndAssert(o("world")))
5360
.testEquals();
5461
}
55-
56-
@Test
57-
public void testSerialization() {
58-
SerializableTester.reserializeAndAssert(s("hello"));
59-
SerializableTester.reserializeAndAssert(s("world"));
60-
SerializableTester.reserializeAndAssert(i(4));
61-
SerializableTester.reserializeAndAssert(i(-6));
62-
}
6362
}

0 commit comments

Comments
 (0)